﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

using Fusion;

namespace Fusion.Tools
{
    public enum EditMode
    {
        Map,
        Texture,
        Animation,
        TileMap,
        Script
    }

    public class Editor
    {
        #region Fields

        // Editors
        public EditMode currentMode = EditMode.Map;
        public Tiler tiler;
        
        // Input
        public Cursor cursor;
        
        public PlayerIndex padIndex = PlayerIndex.One;
        public Controller Pad
        { get { return Input.Pad( padIndex ); } }

        // Dialogs
#if WINDOWS
        public System.Windows.Forms.SaveFileDialog saveDialog = new System.Windows.Forms.SaveFileDialog();
        public System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
#endif
        public double autosaveTimer;

        #endregion

        #region Events

        /// <summary> Add Texture into library as new Image </summary>
        /// <param name="name">The name of the texture file</param>
        public void TextureToLibrary( string name )
        {
			//library.Add( new Image( name ), 3 );
		}


        #endregion

        #region Constructors

        /// <summary> Blank Constructor </summary>
        public Editor()
        {
            cursor = new Cursor();

            #region Library Setup


            // Load all textures into library
            //foreach( KeyValuePair<string, Texture2D> entry in Engine.Textures.data )
            //    library.Add( new Image( entry.Key ), 3 );

            // Register callback for newly created textures
            Engine.Textures.Created += TextureToLibrary;

            #endregion
        }

        #endregion

        #region Methods

        public void Update()
        {
        }

        /// <summary> Renders the scene and appropriate overlays </summary>
        public void Draw()
        {
        }

        #endregion

        #region File

        /// <summary> Choose a path to save map to </summary>
        void ShowSaveMapDialog()
        {
#if WINDOWS
            saveDialog.FileOk += new System.ComponentModel.CancelEventHandler( DoneSaveMapDialog );
            saveDialog.Filter = "Map file|*.map";
            saveDialog.FilterIndex = 0;
            saveDialog.AddExtension = true;
            saveDialog.DefaultExt = "*.map";
            saveDialog.InitialDirectory = "//Maps";

            saveDialog.ShowDialog();
#endif
        }

        /// <summary> Map save path is confirmed, commence file operations </summary>
        void DoneSaveMapDialog( object sender, System.ComponentModel.CancelEventArgs e )
        {
#if WINDOWS
            saveDialog.FileOk -= DoneSaveMapDialog;
            SaveMap( saveDialog.FileName );
#endif
        }


        /// <summary> Choose a path to open map from </summary>
        void ShowOpenMapDialog()
        {
#if WINDOWS
            openDialog.FileOk += new System.ComponentModel.CancelEventHandler( DoneOpenDialog );
            openDialog.Filter = "Map files|*.map";
            openDialog.FilterIndex = 0;
            openDialog.Multiselect = false;
            openDialog.AddExtension = true;
            openDialog.DefaultExt = "*.map";
            openDialog.InitialDirectory = "//Maps";

            openDialog.ShowDialog();
#endif
        }

        /// <summary> Map load path is confirmed, commence file operations </summary>
        void DoneOpenDialog( object sender, System.ComponentModel.CancelEventArgs e )
        {
#if WINDOWS
            openDialog.FileOk -= DoneSaveMapDialog;
            LoadMap( openDialog.FileName );
#endif
        }



        /// <summary> Save a map file to specified file </summary>
        public void SaveMap( string filename )
        {
            Secretary.NewDocument();
            //XElement element = mapper.scene.Write();
            //Secretary.Save( filename, element );
        }

        /// <summary> Choose a path to open map from </summary>
        public void LoadMap( string filename )
        {
            XElement element = Secretary.Load( filename );

            //if( element != null )
                //mapper.scene.Read( element );
        }


        /// <summary> Choose a path to open map from </summary>
        public void SaveLibrary()
        {
            Secretary.NewDocument();

            //XElement element = library.Write();
            //Secretary.Save( "Library.xml", element );
        }

        /// <summary> Choose a path to open map from </summary>
        public void LoadLibrary()
        {
            XElement element = Secretary.Load( "Library.xml" );

            //if( element != null )
            //    library.Read( element );
        }

        #endregion
    }
}
